import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { InfraHolidayApi } from '#/api/infra/calendar/holiday';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'naturalYear',
      label: '自然年度',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'holidayName',
      label: '假期名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入假期名称',
      },
    },
    {
      fieldName: 'holidayType',
      label: '假期类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_HOLIDAY_TYPE, 'number'),
        placeholder: '请选择假期类型',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '假期名称',
      field: 'holidayName',
    },
    {
      label: '假期类型',
      field: 'holidayType',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.INFRA_HOLIDAY_TYPE,
          value: val,
        });
      },
    },
    {
      label: '开始日期',
      field: 'startDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '结束日期',
      field: 'endDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '有效状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },
    {
      label: '备注',
      field: 'remark',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'naturalYear',
      label: '自然年度',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'holidayName',
      label: '假期名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入假期名称',
      },
    },
    {
      fieldName: 'holidayType',
      label: '假期类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.INFRA_HOLIDAY_TYPE, 'number'),
        placeholder: '请选择假期类型',
      },
    },
    {
      fieldName: 'queryDate',
      label: '放假日期',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<InfraHolidayApi.Holiday>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '假期id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'naturalYear',
      title: '自然年度',
      minWidth: 80,
    },
    {
      field: 'holidayName',
      title: '假期名称',
      minWidth: 200,
    },
    {
      field: 'holidayType',
      title: '假期类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_HOLIDAY_TYPE },
      },
    },
    {
      field: 'startDate',
      title: '开始日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '有效状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
